
package com.heimali.happyreader.utils;

public class UserUtils {
    public static final int USERNAME_OK = 100;
    public static final int USERNAME_HAD_USED = 101;
    public static final int USERNAME_NOT_EMAIL = 102;

    public static final int PASSWORD_OK = 200;
    public static final int PASSWORD_TOO_SHORT = 201;
    public static final int PASSWORD_TOO_LONG = 202;

    public static final int USERNAME_OR_PASSWORD_ERROR = 301;

    public static int checkUserName(String username) {
        username = username.trim();
        if (username.indexOf("@") <= 0) {
            return USERNAME_NOT_EMAIL;
        }

        return USERNAME_OK;
    }

    public static int checkPassword(String password) {
        int len = password.length();
        if (len < 6) {
            return PASSWORD_TOO_SHORT;
        } else if (len > 20) {
            return PASSWORD_TOO_LONG;
        }

        return PASSWORD_OK;
    }

    public static String getUserName(String username, String password) {
        return lie(username, password);
    }

    public static String getPassword(String password, String username) {
        return lie(password, username);
    }

    private static String lie(String src, String key) {
        int seed = key.length();
        char[] chars = src.toCharArray();
        for (int len = chars.length, i = 0; i < len; i++) {
            chars[i] = (char) ((int) chars[i] + i + seed * (i % 2 == 1 ? 1 : -1));
        }
        return StringUtils.escape(String.valueOf(chars));
    }
}
